package com.pt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pt.entity.PrivateMessage;
import com.pt.mapper.PrivateMessageMapper;
import com.pt.service.PrivateMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class PrivateMessageServiceImpl implements PrivateMessageService {
    
    private final PrivateMessageMapper privateMessageMapper;
    
    @Override
    public IPage<PrivateMessage> getInbox(Long userId, Page<PrivateMessage> page) {
        QueryWrapper<PrivateMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("receiver_id", userId)
               .eq("receiver_deleted", false)
               .orderByDesc("sent_time");
        return privateMessageMapper.selectPage(page, wrapper);
    }
    
    @Override
    public IPage<PrivateMessage> getOutbox(Long userId, Page<PrivateMessage> page) {
        QueryWrapper<PrivateMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("sender_id", userId)
               .eq("sender_deleted", false)
               .orderByDesc("sent_time");
        return privateMessageMapper.selectPage(page, wrapper);
    }
    
    @Override
    @Transactional
    public PrivateMessage sendMessage(PrivateMessage message) {
        privateMessageMapper.insert(message);
        return message;
    }
    
    @Override
    @Transactional
    public void markAsRead(Long messageId, Long userId) {
        PrivateMessage message = privateMessageMapper.selectById(messageId);
        if (message != null && message.getReceiverId().equals(userId)) {
            message.setIsRead(true);
            message.setReadTime(LocalDateTime.now());
            privateMessageMapper.updateById(message);
        }
    }
    
    @Override
    @Transactional
    public void deleteMessage(Long messageId, Long userId) {
        PrivateMessage message = privateMessageMapper.selectById(messageId);
        if (message != null) {
            if (message.getSenderId().equals(userId)) {
                message.setSenderDeleted(true);
            } else if (message.getReceiverId().equals(userId)) {
                message.setReceiverDeleted(true);
            }
            privateMessageMapper.updateById(message);
        }
    }
    
    @Override
    public Integer getUnreadCount(Long userId) {
        QueryWrapper<PrivateMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("receiver_id", userId)
               .eq("is_read", false)
               .eq("receiver_deleted", false);
        Long count = privateMessageMapper.selectCount(wrapper);
        return count.intValue();
    }
    
    @Override
    public PrivateMessage getMessageById(Long messageId, Long userId) {
        PrivateMessage message = privateMessageMapper.selectById(messageId);
        
        // 检查权限：只有发件人或收件人可以查看
        if (message != null && 
            (message.getSenderId().equals(userId) || message.getReceiverId().equals(userId))) {
            
            // 检查是否已被删除
            if ((message.getSenderId().equals(userId) && message.getSenderDeleted()) ||
                (message.getReceiverId().equals(userId) && message.getReceiverDeleted())) {
                return null;
            }
            
            return message;
        }
        
        return null;
    }
} 